package com.fengmi.miaosha.controller;


import com.fengmi.miaosha.entity.MsGoods;
import com.fengmi.miaosha.service.IMsGoodsService;
import com.fengmi.utils.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhuxm
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/miaosha/goods")
public class MsGoodsController {


    @Autowired
    private IMsGoodsService msGoodsService;


    //准备模拟数据
    @RequestMapping("/gendata")
    public String gendata(){
        //获取最近的5个时间区间
        List<Date> dateMenus = DateUtil.getDateMenus();

        for (Date dateMenu : dateMenus) {
            Random random = new Random();
            int randomv = random.nextInt(200);
            //每个区间插入模拟数据
            MsGoods secKillGoods = new MsGoods();
            secKillGoods.setPrice(399.99);
            secKillGoods.setSeckillPrice(99.99);
            secKillGoods.setSeckillCount(10);
            secKillGoods.setSeckillRemaining(10);
            secKillGoods.setSeckillGoodsTitle("华为p40pro 亮黑色 8+128G全网通 立减"+randomv);
            secKillGoods.setSeckillStatus(1);
            secKillGoods.setCreatedate(new Date());
            secKillGoods.setSeckillStarttime(DateUtil.addDateMinutes(dateMenu,2));
            secKillGoods.setSeckillEndtime(DateUtil.addDateMinutes(dateMenu,60));
            msGoodsService.save(secKillGoods);
        }


        return "success";


    }




    //获取时间菜单
    @RequestMapping("getDateMenu")
    public List<Date> getDateMenu(){

        return DateUtil.getDateMenus();
    }


    @RequestMapping("findGoodsListByTime")
    public List<MsGoods> findGoodsListByTime(String time){


        return msGoodsService.findGoodsListByTime(time);
    }


    @RequestMapping("findGoodsListByTimeFromDB")
    public List<MsGoods> findGoodsListByTimeFromDB(String startTime,String endTime){

        return msGoodsService.findGoodsByTimeFromDB(startTime,endTime);
    }


}
